//
//  CodingNetAPIClient.m
//  Coding_iOS
//
//  Created by 王 原闯 on 14-7-30.
//  Copyright (c) 2014年 Coding. All rights reserved.
//

#define kNetworkMethodName @[@"Get", @"Post", @"Put", @"Delete"]

#import "CodingNetAPIClient.h"
//#import "Login.h"

@implementation CodingNetAPIClient

static CodingNetAPIClient *_sharedClient = nil;
static dispatch_once_t onceToken;

+ (CodingNetAPIClient *)sharedJsonClient {
    dispatch_once(&onceToken, ^{
        _sharedClient = [[CodingNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://mbpcos.com/"]];
    });
    return _sharedClient;
}

+ (id)changeJsonClient{
    _sharedClient = [[CodingNetAPIClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://mbpcos.com/"]];
    return _sharedClient;
}

- (id)initWithBaseURL:(NSURL *)url {
    self = [super initWithBaseURL:url];
    if (!self) {
        return nil;
    }
    self.responseSerializer = [AFJSONResponseSerializer serializer];
    self.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/plain", @"text/javascript", @"text/json", @"text/html", nil];
    
    [self.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    [self.requestSerializer setValue:url.absoluteString forHTTPHeaderField:@"Referer"];
    
    self.securityPolicy.allowInvalidCertificates = YES;
    
    return self;
}
//请求头：
-(void)setHttpRequestheader{
    NSString *token=@"";
    NSDictionary *headerFieldValueDictionary = @{@"":token};
    //self.requestSerializer.timeoutInterval=10.0f;
    
    if (headerFieldValueDictionary != nil) {
        
        for (id httpHeaderField in headerFieldValueDictionary.allKeys) {
            
            id value = headerFieldValueDictionary[httpHeaderField];
            
            if ([httpHeaderField isKindOfClass:[NSString class]] && [value isKindOfClass:[NSString class]]) {
                
                [self.requestSerializer setValue:(NSString *)value forHTTPHeaderField:(NSString *)httpHeaderField];//这种情况设置就成功了。。
                
            } else {
                
                NSLog(@"Error, class of key/value in headerFieldValueDictionary should be NSString.");
                
            }
            
        }
    }
}
- (void)requestJsonDataWithPath:(NSString *)aPath
                     withParams:(NSDictionary*)params
                 withMethodType:(NetworkMethod)method
                       andBlock:(void (^)(id data, NSError *error))block{
    [self requestJsonDataWithPath:aPath withParams:params withMethodType:method autoShowError:YES andBlock:block];
}

- (void)requestJsonDataWithPath:(NSString *)aPath
                     withParams:(NSDictionary*)params
                 withMethodType:(NetworkMethod)method
                  autoShowError:(BOOL)autoShowError
                       andBlock:(void (^)(id data, NSError *error))block{
    if (!aPath || aPath.length <= 0) {
        return;
    }
   
    aPath = [aPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    发起请求
    switch (method) {
        case Get:{
            //所有 Get 请求，增加缓存机制
            NSMutableString *localPath = [aPath mutableCopy];
            if (params) {
                [localPath appendString:params.description];
            }
            [self GET:aPath parameters:params success:^(NSURLSessionDataTask *operation, id responseObject) {
               // DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
                block(responseObject, nil);
           } failure:^(NSURLSessionDataTask *operation, NSError *error) {
             //   block(responseObject, error);
           }];
            break;}
        case Post:{
            [self POST:aPath parameters:params success:^(NSURLSessionDataTask *operation, id responseObject) {
                // DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
                block(responseObject, nil);
            } failure:^(NSURLSessionDataTask *operation, NSError *error) {
                //   block(responseObject, error);
            }];
            break;}
        case Put:{
           //
            break;}
        case Delete:{
            break;}
        default:
            break;
    }
    
}

//-(void)requestJsonDataWithPath:(NSString *)aPath file:(NSDictionary *)file withParams:(NSDictionary *)params withMethodType:(NetworkMethod)method andBlock:(void (^)(id, NSError *))block{
//    //log请求数据
//    DebugLog(@"\n===========request===========\n%@:\n%@", aPath, params);
//    aPath = [aPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
//    
//    // Data
//    NSData *data;
//    NSString *name, *fileName;
//    
//    if (file) {
//        UIImage *image = file[@"image"];
//        // 压缩
//        data = [image dataForCodingUpload];
//        name = file[@"name"];
//        fileName = file[@"fileName"];
//    }
//    
//    switch (method) {
//        case Post:{
//            
//            AFHTTPRequestOperation *operation = [self POST:aPath parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//                if (file) {
//                    [formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpeg"];
//                }
//            } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//                DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
//                id error = [self handleResponse:responseObject];
//                if (error) {
//                    block(nil, error);
//                }else{
//                    block(responseObject, nil);
//                }
//
//            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//                DebugLog(@"\n===========response===========\n%@:\n%@\n%@", aPath, error, operation.responseString);
//                [NSObject showError:error];
//                block(nil, error);
//            }];
//            [operation start];
//            
//            break;
//        }
//        default:
//            break;
//    }
//}

- (void)reportIllegalContentWithType:(IllegalContentType)type
                          withParams:(NSDictionary*)params{
    NSString *aPath;
    switch (type) {
        case IllegalContentTypeTweet:
            aPath = @"/api/inform/tweet";
            break;
        case IllegalContentTypeTopic:
            aPath = @"/api/inform/topic";
            break;
        case IllegalContentTypeProject:
            aPath = @"/api/inform/project";
            break;
        case IllegalContentTypeWebsite:
            aPath = @"/api/inform/website";
            break;
        default:
            aPath = @"/api/inform/tweet";
            break;
    }
//    DebugLog(@"\n===========request===========\n%@:\n%@", aPath, params);
//    [self POST:aPath parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        DebugLog(@"\n===========response===========\n%@:\n%@", aPath, responseObject);
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        DebugLog(@"\n===========response===========\n%@:\n%@\n%@", aPath, error, operation.responseString);
//    }];
}

//- (void)uploadImage:(UIImage *)image path:(NSString *)path name:(NSString *)name
//       successBlock:(void (^)(AFHTTPRequestOperation *operation, id responseObject))success
//       failureBlock:(void (^)(AFHTTPRequestOperation *operation, NSError *error))failure
//      progerssBlock:(void (^)(CGFloat progressValue))progress{
//
//    NSData *data = [image dataForCodingUpload];
//    
//    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
//    formatter.dateFormat = @"yyyyMMddHHmmss";
//    NSString *str = [formatter stringFromDate:[NSDate date]];
//    NSString *fileName = [NSString stringWithFormat:@"%@_%@.jpg", [Login curLoginUser].global_key, str];
//    DebugLog(@"\nuploadImageSize\n%@ : %.0f", fileName, (float)data.length/1024);
//
//    AFHTTPRequestOperation *operation = [self POST:path parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//        [formData appendPartWithFileData:data name:name fileName:fileName mimeType:@"image/jpeg"];
//    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        DebugLog(@"Success: %@ ***** %@", operation.responseString, responseObject);
//        id error = [self handleResponse:responseObject];
//        if (error && failure) {
//            failure(operation, error);
//        }else{
//            success(operation, responseObject);
//        }
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        DebugLog(@"Error: %@ ***** %@", operation.responseString, error);
//        if (failure) {
//            failure(operation, error);
//        }
//    }];
//    
//    [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
//        CGFloat progressValue = (float)totalBytesWritten/(float)totalBytesExpectedToWrite;
//        if (progress) {
//            progress(progressValue);
//        }
//    }];
//    [operation start];
//}
//
//- (void)uploadVoice:(NSString *)file
//           withPath:(NSString *)path
//         withParams:(NSDictionary*)params
//           andBlock:(void (^)(id data, NSError *error))block {
//    
//    if (![[NSFileManager defaultManager] fileExistsAtPath:file]) {
//        return;
//    }
//    
//    NSData *data = [NSData dataWithContentsOfFile:file];
//    NSString *fileName = [file lastPathComponent];
//
//    DebugLog(@"\nuploadVoiceSize\n%@ : %.0f", fileName, (float)data.length/1024);
//    
//    AFHTTPRequestOperation *operation = [self POST:path parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
//        [formData appendPartWithFileData:data name:@"file" fileName:fileName mimeType:@"audio/amr"];
//    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        DebugLog(@"\n===========response===========\n%@:\n%@", path, responseObject);
//        id error = [self handleResponse:responseObject autoShowError:YES];
//        if (error) {
//            block(nil, error);
//        }else{
//            block(responseObject, nil);
//        }
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        DebugLog(@"\n===========response===========\n%@:\n%@", path, error);
//        [NSObject showError:error];
//        block(nil, error);
//    }];
//    
//    [operation start];
//}
@end
